﻿
 



namespace Orid.Management.SOHO.Book.Client.Logic
{
    using System;

    public class UtilBook
    {
        private Service.Client client;

        private readonly string wcfAddress;

        public UtilBook(string address)
        {
            wcfAddress = address;
          
        }

        public Service.Client WcfClient
       {
           get
           {
               if (client == null)
               {
                   try
                   {
                       client = new Service.Client(wcfAddress);
                   }
                   catch
                   {
                       return null;
                   }
               }

               return client;
           }
       }

        public UtilBook(  Service.Client client)
        {
            this.client = client;
        }

        public bool AddSchedule(string operatorID,   DateTime dateFrom, DateTime dateTo)
        {
            bool bResult;

            try
            {
                bResult = WcfClient.AddSchedule(operatorID,   dateFrom, dateTo);
            }
            catch(Exception ex)
            {
                WcfClient.Error(ex.Message);
                return false;
            }

            return bResult;
        }



        private DateTime GetCurrentTime()
        {
            DateTime result = DateTime.MinValue;

            try
            {
                result = WcfClient.GetCurrentTime();
            }
            catch (Exception ex)
            {
                WcfClient.Error(ex.Message);

                return result;
            }

            return result;
        }

        public DateTime GetBookTime()
        {
            DateTime result = DateTime.MinValue;

            try
            {
                result = WcfClient.GetBookTime();

            }
            catch (Exception ex)
            {
                WcfClient.Error(ex.Message);

                return result;
            }

            return result;
        }

        public  bool RemoveSchedule(string operatorID, DateTime dateFrom, DateTime dateTo)
        {
             bool bResult;

            try
            {
                bResult = WcfClient.RemoveSchedule( operatorID,  dateFrom,  dateTo);
            }
            catch(Exception ex)
            {
                WcfClient.Error(ex.Message);

                return false;
            }

            return bResult;
        }

 
         public Book.Service.BookSetting[] GetCurrentBookSetting()
         {
             Book.Service.BookSetting[] bookSettings = null ;

             try
             {


                bookSettings = WcfClient.GetCurrentBookSetting();


             }
             catch (Exception ex)
             {
                 WcfClient.Error(ex.Message);

                 //return bookSettings;
             }


             return bookSettings;
         }

         public Book.Service.BookRecord[] GetBookList(string operatorID, DateTime bookDateFrom,DateTime bookDateTo)
         {
             Book.Service.BookRecord[] bookRecords = null;

             try
             {
                 bookRecords = WcfClient.GetBookList(operatorID, bookDateFrom,bookDateTo);

             }
             catch (Exception ex)
             {
                 WcfClient.Error(ex.Message);
                  
             }
             
             return bookRecords;
         }

         public Book.Service.BookRecord[] GetBookList(string operatorID, DateTime bookDate )
         {
             Book.Service.BookRecord[] bookRecords = null;

             try
             {
                 bookRecords = WcfClient.GetBookList(operatorID, bookDate );

             }
             catch (Exception ex)
             {
                 WcfClient.Error(ex.Message);

             }

             return bookRecords;
         }


         
         public Book.Service.BookRecord GetNextWorkTime(string operatorID)
         {
             Book.Service.BookRecord  bookRecord  = null;

             try
             {
                 bookRecord = WcfClient.GetNextWorkTime(operatorID, GetCurrentTime());

             }
             catch (Exception ex)
             {
                 WcfClient.Error(ex.Message);

             }

             return bookRecord ;
         }

         public DateTime[] GetBookDays(string operatorID, DateTime dateFrom, DateTime dateTo)
         {
             DateTime[] bookDays = null;

             try
             {
                 bookDays = WcfClient.GetBookDays(operatorID, dateFrom, dateTo);

             }
             catch (Exception ex)
             {
                 WcfClient.Error(ex.Message);
                 
             }
              

             return bookDays;
         }

    }
}
